package org.example.AI;

/**
 * @author: 默苍璃
 * @date: 2024-07-18 11:20
 */

import java.util.*;

public class SimpleExpressionParser {

    public static void main(String[] args) {
        String expression = "9+9-9+6"; // 示例表达式
        try {
            double result = evaluate(expression);
            System.out.println("计算结果: " + result);
        } catch (Exception e) {
            System.err.println("解析表达式时出错: " + e.getMessage());
        }
    }

    public static double evaluate(String expression) throws Exception {
        Deque<Double> operands = new LinkedList<>();
        Deque<Character> operators = new LinkedList<>();

        for (int i = 0; i < expression.length(); i++) {
            char ch = expression.charAt(i);
            if (Character.isWhitespace(ch)) {
                continue;
            } else if (Character.isDigit(ch)) {
                StringBuilder sb = new StringBuilder();
                while (Character.isDigit(ch) || ch == '.') {
                    sb.append(ch);
                    i++;
                    if (i < expression.length()) {
                        ch = expression.charAt(i);
                    } else {
                        break;
                    }
                }
                i--; // 因为for循环中还会自增，所以需要减一
                operands.push(Double.parseDouble(sb.toString()));
            } else if (ch == '(') {
                operators.push(ch);
            } else if (ch == ')') {
                while (!operators.isEmpty() && operators.peek() != '(') {
                    operands.push(applyOp(operators.pop(), operands.pop(), operands.pop()));
                }
                if (!operators.isEmpty()) {
                    operators.pop(); // 弹出'('
                } else {
                    throw new Exception("不匹配的括号");
                }
            } else if (isOperator(ch)) {
                while (!operators.isEmpty() && hasPrecedence(ch, operators.peek())) {
                    operands.push(applyOp(operators.pop(), operands.pop(), operands.pop()));
                }
                operators.push(ch);
            } else {
                throw new Exception("无效的字符: " + ch);
            }
        }

        while (!operators.isEmpty()) {
            operands.push(applyOp(operators.pop(), operands.pop(), operands.pop()));
        }

        return operands.pop();
    }

    private static boolean isOperator(char op) {
        return op == '+' || op == '-' || op == '*' || op == '/';
    }

    private static boolean hasPrecedence(char op1, char op2) {
        if ((op2 == '*' || op2 == '/') && (op1 == '+' || op1 == '-')) {
            return false;
        } else {
            return true;
        }
    }

    private static double applyOp(char op, double b, double a) throws Exception {
        switch (op) {
            case '+':
                return a + b;
            case '-':
                return a - b;
            case '*':
                return a * b;
            case '/':
                if (b == 0) {
                    throw new Exception("除数不能为0");
                }
                return a / b;
            default:
                throw new Exception("未知的操作符");
        }
    }
}
